#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"

#include "driver/uart.h"
#include "driver/gpio.h"
#include "string.h"
#include "LCD.h"
#include "Sensor.h"

#define LINE_LEN                32              //数据长度
static uint8_t temp_buff[LINE_LEN];                    //从机向主机发送数据BUFF


#define EX_UART_NUM UART_NUM_1
#define ECHO_TEST_TXD (GPIO_NUM_27)
#define ECHO_TEST_RXD (GPIO_NUM_14)
#define BUF_SIZE (1024)
#define RX0_BUF_SIZE 		(1024)
#define TX0_BUF_SIZE 		(512)
static const char *TAG = "UART1";

static void echo_task(void *arg);


void Uart1_LCD_init(void)
{
    //串口配置结构体
    uart_config_t uart_config = {
        .baud_rate = 115200,                   //波特率
        .data_bits = UART_DATA_8_BITS,         //数据位
        .parity = UART_PARITY_DISABLE,         //校验位
        .stop_bits = UART_STOP_BITS_1,         //停止位
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, //流控位
        .source_clk = UART_SCLK_APB,
    };

    //设置串口
    uart_param_config(EX_UART_NUM, &uart_config);
    //IO映射->TX->GPIO_NUM_4,RX->GPIO_NUM_5
    uart_set_pin(EX_UART_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    //注册串口服务即使能+设置缓存区大小
    uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
}



//-------------------------------------------------------------------------------------------------------------------
//  @brief      根据协议处理数据
//  @param      void
//  @return     void
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void Send_Lcd_Data(char Address_High,char Address_Low,int16_t Sensor_Data)
{

    temp_buff[0] = 0x5A;                         //帧头
    temp_buff[1] = 0xA5;                         //帧头

    temp_buff[2] = 0X05;                        //数据长度
    temp_buff[3] = 0X82;                        //功能字82写， 83读

    temp_buff[4] = Address_High;                         //地址低8位

    temp_buff[5] = Address_Low;                         //地址低八位

    temp_buff[6] = Sensor_Data>>8;          //从1000读取个字节长度   高8

    temp_buff[7] = Sensor_Data&0xFF;        //从1000读取个字节长度   低八位

    uart_write_bytes(EX_UART_NUM, &temp_buff, LINE_LEN);

}